//
//  ZMToolBar.swift
//  ZMWeiboSwift
//
//  Created by pg on 2017/7/31.
//  Copyright © 2017年 pg. All rights reserved.
//

import UIKit

enum ToolBarButtonType : Int {
    //  图片
    case picture = 0
    //  @
    case mention = 1
    //  话题
    case trend = 2
    //  表情
    case emoticon = 3
    //  加号
    case add = 4
}


class ZMToolBar: UIStackView {

    //按钮点击事件  执行闭包
    var toolBarButtonClickClosure: ((UIButton,ToolBarButtonType)->())?
    
    
    //设置工具条的方向
    var toolBarDircation: UILayoutConstraintAxis = .horizontal{
        didSet{
            
            self.axis = toolBarDircation
            
        }
    }
   //设置子视图的填充方向
    var toolBarDistribution : UIStackViewDistribution = .fillEqually{
        didSet{
            self.distribution = toolBarDistribution
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
       
        self.backgroundColor = UIColor.white
        
        toolBarDircation = .horizontal
        toolBarDistribution = .fillEqually
        
        addChildButton(title:"",imageName: "compose_toolbar_picture",type: .picture)
        addChildButton(title:"",imageName: "compose_mentionbutton_background",type: .mention)
        addChildButton(title:"",imageName: "compose_trendbutton_background",type: .trend)
        addChildButton(title:"",imageName: "compose_emoticonbutton_background",type: .emoticon)
        addChildButton(title:"",imageName: "compose_add_background",type: .add)

        
    }
    //添加子空间
   // private var index : Int = 0
    private func addChildButton(title: String , imageName: String,type: ToolBarButtonType){
       let btn  = UIButton()
        btn.setImage(UIImage(named:imageName), for: UIControlState.normal)
        btn.setTitle(title, for: UIControlState.normal)
        //取消 高亮效果
        btn.adjustsImageWhenHighlighted = false
        btn.tag = type.rawValue
        
        btn.backgroundColor = UIColor.white
        
        addArrangedSubview(btn)
        
        //添加单击事件
        btn.addTarget(self, action: #selector(btnClick(sender:)), for: UIControlEvents.touchUpInside)
    }
    
    //按钮单击事件
    @objc private func btnClick(sender: UIButton){
        
        let type = ToolBarButtonType(rawValue: sender.tag)
        
        toolBarButtonClickClosure?(sender,type!)
        
    }
    
    
}
